package com.lims;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author limaosheng
 * @create 2023-05-23 16:22
 * @desc
 */
public class 更新小脚本 {

    public final static String DB_URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8";
    public final static String DB_USER = "root";
    public final static String DB_PWD = "123456";

    public static void main(String[] args) throws Exception{
        int i = 1;
        String s = "";
        File file = new File("D:\\结果信息\\查询结果.txt");
        InputStreamReader in = new InputStreamReader(new FileInputStream(file), "UTF-8");
        BufferedReader br = new BufferedReader(in);
        while ((s = br.readLine()) != null) {
            System.out.println(i++ + ":" + s);
            Connection conn = null;
            try {
                conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PWD);
                try {
                    String[] farr = s.split("，");
                    String sql = "UPDATE `test`.`aaaa` SET `count` = "+ farr[0].substring(4) +" WHERE `id` = " + farr[1].substring(5);
                    PreparedStatement ps2 = conn.prepareStatement(sql);
                    ps2.executeUpdate();
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println(s);
                }
            } catch (Exception throwables) {
                throwables.printStackTrace();
            } finally {
                closeConn(conn);
            }
        }
    }

    public static void closeConn(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
